package io.renren.modules.app.controller;

import java.awt.*;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

import com.alibaba.fastjson.JSONObject;
import com.zheok.testing.vo.LableVo;
import io.renren.modules.app.service.ProductService;
import io.renren.modules.app.service.TranslateUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import com.zheok.testing.entity.ProductEntity;
import org.springframework.web.client.RestTemplate;
import utils.R;
import io.renren.common.utils.PageUtils;



/**
 * 产品表
 *
 * @author ææ¢
 * @email 2863751700@qq.com
 * @date 2022-07-31 19:06:14
 */
@Slf4j
@RestController
@RequestMapping("testing/product")
public class ProductController {
    @Autowired
    private ProductService productService;

    /**
     * 列表
     */
    @RequestMapping("/list")
   // // //@RequiresPermissions("testing:product:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = productService.queryPage(params);

        return R.ok().put("page", page);
    }


    @GetMapping("/lable/{catagory}")
    public R list(@PathVariable("catagory") String catagory){
        List<LableVo>  lableVos =productService.getCatagory2(catagory);

        return R.ok().put("data", lableVos);
    }
    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
   // // //@RequiresPermissions("testing:product:info")
    public R info(@PathVariable("id") Long id){
		ProductEntity product = productService.getById(id);

        return R.ok().put("product", product);
    }
    @Value("${en_url}")
    String en_url;
    @Autowired
    private RestTemplate restTemplate;
    /**
     * 保存
     */
    @RequestMapping("/save")
   // // //@RequiresPermissions("testing:product:save")
    public R save(@RequestBody ProductEntity product) throws IOException {
		productService.saveinfo(product);
        //TODO 发送请求添加英文数据
//        String url =en_url+"/product/save";
//        String transResult = TranslateUtil.getTransResult(product.getProductName(), "auto", "en");
//        product.setProductName(transResult);
//        String param = TranslateUtil.getTransResult(product.getProductParam(), "auto", "en");
//        product.setProductParam(param);
//        String label = TranslateUtil.getTransResult(product.getProductLable(), "auto", "en");
//        product.setProductLable(label);
//        JSONObject postData = (JSONObject) JSONObject.toJSON(product);
//        String body = restTemplate.postForEntity(url, postData, String.class).getBody();
//        log.info("调用英文接口的结果body:{}",body);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
   // // //@RequiresPermissions("testing:product:update")
    public R update(@RequestBody ProductEntity product){
		productService.saveinfo(product);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
   // // //@RequiresPermissions("testing:product:delete")
    public R delete(@RequestBody Long[] ids){
		productService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
